package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.MsgDialog;

import java.util.HashMap;

public class ComplaintsActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout llCause;
    private EditText etCause;
    private String cause;
    private String targetId;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaints);
        llCause = (LinearLayout) findViewById(R.id.ll_cause);
        etCause = (EditText) findViewById(R.id.et_cause);
        Intent intent = getIntent();
        targetId = intent.getStringExtra("id");
        type = intent.getStringExtra("type");

        for (int i = 0; i < llCause.getChildCount(); i++) {
            View v = llCause.getChildAt(i);
            if (v instanceof TextView) {
                v.setOnClickListener(this);

            }
        }


        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getCause();
                String otherCause = etCause.getText().toString();
                if (TextUtils.isEmpty(otherCause) && TextUtils.isEmpty(cause)) {
                    MsgDialog dialog = new MsgDialog(ComplaintsActivity.this, "请选择投或者填写投诉原因", null);
                    dialog.show();
                    return;
                }
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("targetId", targetId);
                params.put("type", type);
                if (TextUtils.isEmpty(cause))
                    params.put("desc", otherCause);
                if (TextUtils.isEmpty(otherCause))
                    params.put("desc", cause);
                if (!TextUtils.isEmpty(cause) && !TextUtils.isEmpty(otherCause))
                    params.put("desc", cause + "\n" + otherCause);
                params.put("acc_token", UserTools.getUser(ComplaintsActivity.this).getAcc_token());
                VolleyTool.get(Constants.URL_GET_COMPLAINTS, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        BaseBean bean = (BaseBean) t;
                        if (bean.getCode() >= 0) {

                            MsgDialog dialog = new MsgDialog(ComplaintsActivity.this, "感谢您的反馈\n我们会尽快为您解决问题", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    finish();
                                }
                            });
                            dialog.show();

                        } else {
                            showShortMsg(bean.getMessage());
                        }

                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {

                    }
                }, 0, BaseBean.class);


            }
        });
    }

    @Override
    public void onClick(View v) {
        v.setSelected(!v.isSelected());
        if (v.isSelected()) {
            for (int i = 0; i < llCause.getChildCount(); i++) {
                View vv = llCause.getChildAt(i);
                if (v != vv) {
                    vv.setSelected(false);
                }
            }

        }
    }

    public String getCause() {
        for (int i = 0; i < llCause.getChildCount(); i++) {
            View v = llCause.getChildAt(i);
            if (v instanceof TextView && v.isSelected()) {
                cause = ((TextView) v).getText().toString();
            }
        }
        return cause;
    }
}
